
function dbus() {
this.id = '';
this.callmap = {};
this.stop_timer = false;
this.start = function () {
    this.stop_timer = false;
    this.timer_id = window.setTimeout(this.do_pingpong, 2000, this);
    console.log('start timer:' + this.timer_id)
}
this.stop = function () {
    console.log('clear timer:' + this.timer_id)
    clearTimeout(this.timer_id);
    this.stop_timer = true;
}
this.do_pingpong = function (_this) {
    console.log(_this)
    $.get('/pingpong.do',
        {},
        function(raw) { _this.setup_next_pingpong(raw)}
        );
}
this.setup_next_pingpong = function (raw) {
    var resp = eval(raw);
    var state = resp[0];
    var msg = resp[1];
    this.notify(msg);
    if (!this.stop_timer)
        this.timer_id = setTimeout(this.do_pingpong, 2000, this);
}
this.register = function (msg, callback) {
    if (!this.callmap.hasOwnProperty(msg))
        this.callmap[msg] = [];
    this.callmap[msg].push(callback);
}

this.unregister = function (msg, callback) {
    if (!this.callmap.hasOwnProperty(msg))
        this.callmap[msg] = [];
    var pos = this.callmap[msg].indexOf(callback);
    if (pos == -1)
        return 
    this.callmap[msg].splice(pos, 1);
}

this.notify  = function (msg) {
    var call_list = this.callmap[msg.name];
    for (var i = 0; i < call_list.length; ++i) {
        call_list[i].apply(this, [msg]);
    }
}

}
